<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备名称" prop="devName">
      <el-input v-model="dataForm.devName" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="令牌" prop="runToken">
      <el-input v-model="dataForm.runToken" placeholder="令牌"></el-input>
    </el-form-item>
    <el-form-item label="设备厂商" prop="detail">
      <el-input v-model="dataForm.detail" placeholder="设备厂商"></el-input>
    </el-form-item>
    <el-form-item label="是否可用" prop="enable">
      <el-input v-model="dataForm.enable" placeholder="是否可用"></el-input>
    </el-form-item>
    <el-form-item label="设备当前状态：已激活 未激活 连接硬件才行" prop="status">
      <el-input v-model="dataForm.status" placeholder="设备当前状态：已激活 未激活 连接硬件才行"></el-input>
    </el-form-item>
    <el-form-item label="用处：箱子还是背包" prop="devUsing">
      <el-input v-model="dataForm.devUsing" placeholder="用处：箱子还是背包"></el-input>
    </el-form-item>
    <el-form-item label="设备主人" prop="parentCid">
      <el-input v-model="dataForm.parentCid" placeholder="设备主人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          devId: 0,
          devName: '',
          runToken: '',
          detail: '',
          enable: '',
          status: '',
          devUsing: '',
          parentCid: '',
          createTime: ''
        },
        dataRule: {
          devName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          runToken: [
            { required: true, message: '令牌不能为空', trigger: 'blur' }
          ],
          detail: [
            { required: true, message: '设备厂商不能为空', trigger: 'blur' }
          ],
          enable: [
            { required: true, message: '是否可用不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '设备当前状态：已激活 未激活 连接硬件才行不能为空', trigger: 'blur' }
          ],
          devUsing: [
            { required: true, message: '用处：箱子还是背包不能为空', trigger: 'blur' }
          ],
          parentCid: [
            { required: true, message: '设备主人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.devId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.devId) {
            this.$http({
              url: this.$http.adornUrl(`/product/dev/info/${this.dataForm.devId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.devName = data.dev.devName
                this.dataForm.runToken = data.dev.runToken
                this.dataForm.detail = data.dev.detail
                this.dataForm.enable = data.dev.enable
                this.dataForm.status = data.dev.status
                this.dataForm.devUsing = data.dev.devUsing
                this.dataForm.parentCid = data.dev.parentCid
                this.dataForm.createTime = data.dev.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/dev/${!this.dataForm.devId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'devId': this.dataForm.devId || undefined,
                'devName': this.dataForm.devName,
                'runToken': this.dataForm.runToken,
                'detail': this.dataForm.detail,
                'enable': this.dataForm.enable,
                'status': this.dataForm.status,
                'devUsing': this.dataForm.devUsing,
                'parentCid': this.dataForm.parentCid,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
